package com.nondo.dean.order.service;

import com.nondo.dean.common.tool.BeanUtils;
import com.nondo.dean.common.tool.StringUtils;
import com.nondo.dean.order.domain.Bank;
import com.nondo.dean.order.domain.BankExample;
import com.nondo.dean.order.mapper.BankMapper;
import com.nondo.dean.order.model.BankBean;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaofan
 * @Description: 银行维护业务处理
 * @throws
 * @date 2018-06-1417:06
 * @Version
 */
@Component
public class BankService {

    @Autowired
    private BankMapper bankMapper;

    /**
     * 新增
     * @param bank
     * @return
     */
    public int add(Bank bank){
        return bankMapper.insertSelective(bank);
    }

    /**
     * 修改
     * @param bank
     * @return
     */
    @Transactional
    public int update(Bank bank){
        return bankMapper.updateByPrimaryKeySelective(bank);
    }

    /**
     * 删除
     * @param bank
     * @return
     */
    public int delete(Bank bank){
        bank.setDelFlag(Bank.DEL_FLAG_DELETE);
        return  bankMapper.updateByPrimaryKeySelective(bank);
    }

    /**
     * 按id查询
     * @param id
     * @return
     */
    public Bank select(Integer id){
        return  bankMapper.selectByPrimaryKey(id);
    }
    /**
     * 统计
     * @param bank
     * @return
     */
    public Long count(Bank bank){
        BankExample bankExample = new  BankExample();
        BankExample.Criteria criteria= bankExample.createCriteria();
        setCriteria(criteria,bank);
        return bankMapper.countByExample(bankExample);
    }


    /**
     * 设置查询条件
     *
     * @param criteria
     * @param bank
     */
    void setCriteria(BankExample.Criteria criteria, Bank bank) {
        if (bank != null) {
            if (bank.getId() != null) {
                criteria.andIdEqualTo(bank.getId());
            }
            if (StringUtils.isNoneBlank(bank.getBankname())) {
                criteria.andBanknameEqualTo(bank.getBankname());
            }
            if (StringUtils.isNoneBlank(bank.getName())) {
                criteria.andNameEqualTo(bank.getName());
            }
            if (StringUtils.isNoneBlank(bank.getSapBankSubject())) {
                criteria.andSapBankSubjectEqualTo(bank.getSapBankSubject());
            }

        }
        criteria.andDelFlagEqualTo(Bank.DEL_FLAG_NORMAL);
    }

    /**
     * 获取列表
     * @param offset
     * @param limit
     * @param bank
     * @return
     */
    public List<Bank> getList(int offset,int limit,Bank bank){
        List<Bank> newList = new ArrayList<>();
        RowBounds rowBounds = new RowBounds(offset,limit);
        BankExample example = new BankExample();
        BankExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria,bank);
        newList =bankMapper.selectByExampleWithRowbounds(example,rowBounds);

        return newList;
    }

    /**
     * 按id查询
     * @param id
     * @return
     */
    public BankBean selectBank(Integer id){
        BankBean beans = new BankBean();
        Bank bank =bankMapper.selectByPrimaryKey(id);
        BeanUtils.copy(bank,beans);
        return beans;
    }

}
